package ua.simplecoding.kievhome.converter;

import org.apache.commons.io.FileUtils;
import org.springframework.core.io.UrlResource;
import org.springframework.web.util.UrlPathHelper;
import sun.net.util.URLUtil;
import ua.simplecoding.kievhome.domain.Appartment;
import ua.simplecoding.kievhome.vo.AppartmentSummaryVO;

import java.io.*;
import java.net.URL;

/**
 * User: dmytro.fedotov
 * Date: 03.07.11
 */
public class AppartmentSummaryVOConverter extends AbstractVOConverter<Appartment, AppartmentSummaryVO> {

    private AppartmentDetailsVOConverter appartmentDetailsVOConverter = new AppartmentDetailsVOConverter();
    private AppartmentAddressVOConverter appartmentAddressVOConverter = new AppartmentAddressVOConverter();

    @Override
    public AppartmentSummaryVO convert(Appartment appartment) {
        AppartmentSummaryVO summaryVO = new AppartmentSummaryVO();
        summaryVO.setAppartmentDetailsVO(appartmentDetailsVOConverter.convert(appartment.getAppartmentDetails()));
        summaryVO.setAppartmentAddressVO(appartmentAddressVOConverter.convert(appartment.getAppartmentAddress()));
        summaryVO.setAppartmentId(appartment.getPk());

        // temporary download photo from classpath

        BufferedInputStream is = null;
        try {
            UrlResource resource = new UrlResource(getClass().getClassLoader().getResource("images/image1.jpg"));
            is = new BufferedInputStream(resource.getInputStream());
            byte[] photo = new byte[is.available()];
            is.read(photo);
            summaryVO.setMainPhoto(photo);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {e.printStackTrace();}
            }
        }
        return summaryVO;
    }
}
